package com.mspbots.web.core;

import com.mspbots.web.core.model.TenantUser;
import com.mspbots.web.core.service.TenantService;
import com.mspbots.web.params.TenantParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * com.mspbots.web.core.TenantController
 * Automatically upgrade existing classes to use more advanced API
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/7/9
 */
@Log4j2
@RequiredArgsConstructor
@RestController
@RequestMapping("tenant")
public class TenantController {

    private final TenantService service;

    @GetMapping(path = "all")
    public Flux all() {
        return this.service.loadAll();
    }

    @GetMapping("/users")
    public Mono tenantUser(@RequestParam Map<String, Object> params) {
        return this.service.tenantUser(params);
    }

    @GetMapping("/users/list")
    public Flux<TenantUser> tenantUserList(@RequestParam Map<String, Object> params) {
        return this.service.tenantUserList(params);
    }

    @GetMapping("/id/{tenantId}")
    public Mono findByTenantId(@PathVariable Long tenantId) {
        return this.service.findByTenantId(tenantId);
    }

    @PostMapping
    public Mono insert(@RequestBody TenantParam entity) {
        return this.service.create(entity);
    }

    @PutMapping(path = "{id}")
    public Mono update(@PathVariable Long id, @RequestBody TenantParam entity) {
        return this.service.update(id, entity);
    }

    @PostMapping("delete-tenant/{tenantId}")
    public Mono<Boolean> deleteTenant(@PathVariable long tenantId) {
        return this.service.deleteTenant(tenantId);
    }
}
